package com.xbongbong.workflow.producer.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.workflow.binders.WorkflowMqProducerBinder;
import com.xbongbong.workflow.pojo.WorkflowRabbitmqDTO;
import com.xbongbong.workflow.producer.WorkflowProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * workflow生产者
 * @author 魏荣杰
 *
 */
@Service("workflowProducer")
@EnableBinding({WorkflowMqProducerBinder.class})
public class WorkflowProducerImpl implements WorkflowProducer {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowProducerImpl.class);

    @Resource
    private WorkflowMqProducerBinder workflowMqProducerBinder;

    @Override
    public void startWorkflow(WorkflowRabbitmqDTO workflowRabbitmqDTO) {
        LOGGER.warn("workflow start xbbreftype = " + XbbRefTypeEnum.getByCode(workflowRabbitmqDTO.getWorkflowTransferPOJO().getData().getBusinessType()) + "\n ---data=  " + JSON.toJSONString(workflowRabbitmqDTO.getWorkflowTransferPOJO().getData()));

        MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
//        messageHeaderAccessor.setHeader();
        MessageBuilder messageBuilder = MessageBuilder.withPayload(workflowRabbitmqDTO).setHeaders(messageHeaderAccessor);
        workflowMqProducerBinder.workflowStartOutput().send(messageBuilder.build());
    }

    @Override
    public void startOperatorWorkflow(WorkflowRabbitmqDTO workflowRabbitmqDTO) {
        MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
//        messageHeaderAccessor.setHeader();
        MessageBuilder messageBuilder = MessageBuilder.withPayload(workflowRabbitmqDTO).setHeaders(messageHeaderAccessor);
        workflowMqProducerBinder.workflowOperatorOutput().send(messageBuilder.build());
    }


    @Override
    public void startWorkflowNoApprove(WorkflowRabbitmqDTO workflowRabbitmqDTO) {
        MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
        MessageBuilder messageBuilder = MessageBuilder.withPayload(workflowRabbitmqDTO).setHeaders(messageHeaderAccessor);
        workflowMqProducerBinder.workflowOperatorNoApproveOutput().send(messageBuilder.build());
    }
}
